<template>
  <view>
    <view class="search-container">
      <my-search @xxx="gotoSearch"></my-search>
    </view>
    
    
    <!-- 轮播图 -->
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    
    <!-- 菜单导航 -->
    <view class="nav-list">
      <view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    
    <!-- 楼层导航 -->
    <view class="floor-list">
      <view class="floor-item" v-for="(item, i) in floorList" :key="i">
        <!-- 楼层title -->
        <view class="floor-title">
          <image :src="item.floor_title.image_src"></image>
        </view>
        <!-- 楼层body -->
        <view class="floor-body">
          <!-- body left -->
          <navigator class="floor-body-left" :url="item.product_list[0].url">
            <image
              :src="item.product_list[0].image_src"
              :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode="widthFix"
            >
            </image>
          </navigator>
          <!-- body right -->
          <view class="floor-body-right">
            <navigator
              class="floor-body-item"
              :url="item2.url"
              v-for="(item2, i2) in item.product_list"
              v-if="i2>0"
              :key="i2"
            >
              <image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
          
        </view>
      </view>
    </view>
    
  </view>
</template>

<script>
  // 导入mixin
  import badgeMixin from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMixin],
    data() {
      return {
        swiperList: [], // 轮播图数据容器
        navList: [], // 菜单导航数据
        floorList: [], // 楼层导航数据
      };
    },
    onLoad() {
      // 发起请求
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      // 获取轮播图数据
      async getSwiperList(){
        const {data: res} = await uni.$http.get('/home/swiperdata')
        // console.log('res==>', res)
        // 数据异常判断
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 设置页面数据
        this.swiperList = res.message
      },
      // 获取分类导航数据
      async getNavList(){
        const {data: res} = await uni.$http.get('/home/catitems')
        // console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面数据
        this.navList = res.message
      },
      // 获取楼层数据
      async getFloorList(){
        const {data: res} = await uni.$http.get('/home/floordata')
        console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // 设置页面数据
        // 处理返回数据格式
        res.message.forEach(floor => {
          floor.product_list.forEach(prod => {
            prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      
      // 导航事件处理函数
      navClickHandler(item){
        console.log(item)
        if(item.name === '分类'){
          // 切换到分类tab
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      gotoSearch(e){
        // 拿到一些数据，跳转到搜索页
        console.log('====', e)
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
    }
  }
</script>

<style lang="scss">
  .search-container {
    position: sticky;
    top: 0;
    left: 0;
    z-index: 9;
  }
  
  .swiper {
    height: 330rpx;
    
    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }
  
  .nav-list {
    display: flex;
    justify-content: space-around;
    padding-top: 32rpx;
    margin-bottom: 32rpx;
    .nav-item {
      // margin-top: 32rpx;
      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }
  
  .floor-list {
    
    .floor-item {
      
      .floor-title {
        
        image {
          height: 60rpx;
          width: 100%;
        }
      }
      .floor-body {
        display: flex;
        
        .floor-body-left {
          margin-left: 8rpx;
        }
        
        .floor-body-right {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
        }
      }
    }
  }
</style>
